package com.hcy.schedulingapi.controller;

import com.hcy.rabbitbasicutils.utils.RedisUtils;
import com.hcy.schedulingclient.model.driver.request.UploadLocationRequestDTO;
import com.hcy.rabbitbasicmodel.enums.Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.signl.Add;
import com.hcy.rabbitbasicmodel.signl.Delete;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/1/13 18:36
 */
@RestController
@RequestMapping("/upload")
public class UploadLocationController {

    Logger logger = LoggerFactory.getLogger(UploadLocationController.class);

    /**
     * redis geo key
     */
    @Value("${CAR_GEO_KEY}")
    private String CAR_GEO_KEY;
    @Value("${CAR_GEO_KEY_EXPIRE}")
    private String CAR_GEO_KEY_EXPIRE;
    @Value("${CAR_RUNTIME}")
    private String CAR_RUNTIME_STATUS;

    @Autowired
    RedisUtils redisUtils;

    /**
     * @Author huchenying
     * @Description 车辆上传位置信息,和车辆可接单标识
     * @Date  2022/1/13 18:47
     **/
    @PostMapping("/carLocation")
    public ResponseData<Object> upload(@Validated({Add.class}) @RequestBody UploadLocationRequestDTO requestDTO){
        logger.info("upload start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            //添加坐标
            redisUtils.addGeo(CAR_GEO_KEY,requestDTO.getTid().toString(),requestDTO.getLat(),requestDTO.getLog());
            //添加key的有效时间
            long second = LocalDateTime.now().plusSeconds(30).toEpochSecond(ZoneOffset.of("+8"));
            redisUtils.zAdd(CAR_GEO_KEY_EXPIRE,requestDTO.getTid().toString(),second);
//            redisUtils.zRange(CAR_GEO_KEY_EXPIRE,second,second);
            responseData.setCode(Code.Success);
            return responseData;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("redis位置添加失败",e);
            responseData.setCode(Code.Fail);
            responseData.setMsg("添加位置失败！");
            return responseData;
        }
    }

    @PostMapping("/carDownLine")
    public ResponseData<Object> remove(@Validated({Delete.class}) @RequestBody UploadLocationRequestDTO requestDTO){
        logger.info("remove start param -> {}",ToStringBuilder.reflectionToString(requestDTO));
        ResponseData<Object> responseData = new ResponseData<>();
        try {
            redisUtils.deleteGeo(CAR_GEO_KEY,String.valueOf(requestDTO.getTid()));
            responseData.setCode(Code.Success);
            return responseData;
        } catch (Exception e) {
            logger.error("删除失败!",e);
            responseData.setCode(Code.Fail);
            responseData.setMsg("删除失败");
            return responseData;
        }
    }
}
